package com.aiyige.page.publish.inputtitle;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.utils.ExcludeEmojiInputFilter;
import com.aiyige.utils.InputNumFilter;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = ARouterConfig.SingleLineInputPage)
public class SingleLineInputPage extends AppCompatActivity {
    public static final String EXTRA_KEY_INPUT_STRING = "com.aiyige.extra.EXTRA_KEY_INPUT_STRING";
    @BindView(R.id.titleBackBtn)
    ImageView titleBackBtn;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.titleActionBtn)
    Button titleActionBtn;
    @BindView(R.id.inputEt)
    EditText inputEt;
    @BindView(R.id.inputNumTv)
    TextView inputNumTv;
    @BindView(R.id.maxInputNumTv)
    TextView maxInputNumTv;
    @BindView(R.id.deleteBtn)
    ImageView deleteBtn;
    InputNumFilter inputNumFilter;
    @Autowired
    String pageTitle = "";
    @Autowired
    String hint = "";
    @Autowired
    int maxInputNum;
    @Autowired
    String action = "";
    @Autowired
    String text = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_single_line_input);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        titleTv.setText(pageTitle);
        titleActionBtn.setText(action);
        inputNumFilter = new InputNumFilter(maxInputNum, false);
        inputNumFilter.setListener(new InputNumFilter.Listener() {
            @Override
            public void inputNum(int num) {
                inputNumTv.setText(String.valueOf(num));
            }
        });
        inputEt.setFilters(new InputFilter[]{new ExcludeEmojiInputFilter(),inputNumFilter});
        inputEt.setText(text);
        inputEt.setHint(hint);
        inputEt.setSelection(inputEt.getText().length());
        inputEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    deleteBtn.setVisibility(View.VISIBLE);
                } else {
                    deleteBtn.setVisibility(View.INVISIBLE);
                }
            }
        });
        maxInputNumTv.setText(String.valueOf(maxInputNum));
    }

    @OnClick({R.id.titleBackBtn, R.id.titleActionBtn, R.id.deleteBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titleBackBtn: {
                setResult(RESULT_CANCELED);
                finish();
                break;
            }
            case R.id.titleActionBtn: {
                if (inputEt.getText().length() > 0) {
                    Intent intent = new Intent();
                    intent.putExtra(EXTRA_KEY_INPUT_STRING, inputEt.getText().toString());
                    setResult(RESULT_OK, intent);
                    finish();
                }
                break;
            }
            case R.id.deleteBtn: {
                inputEt.setText("");
                break;
            }
        }
    }
}
